<?php

namespace tools\lib\main\model;

use tools\lib\main\Common;

/**
 * 创建model
 * Class Model
 * @package app\common\command\create
 */
class Model extends Common
{
    /**
     * 自读字段
     * @var array
     */
    private $readonly = [];

    /**
     * 设置主键
     * @var string
     */
    private $pk = 'id';

    /**
     * 设置model类的属性
     * @var array
     */
    private $property = [];

    /**
     * @return string
     */
    public function template()
    {
        $this->getAttr();

        $template = <<<EOT
<?php

{$this->getFileNamespace()}

{$this->getFileUse()}

{$this->getProperty()}
class {$this->className()} {$this->getFileExtends()}
{
    protected \$table = '{$this->parameter->getName()}';
EOT;

        // 插入 schema https://www.kancloud.cn/manual/thinkphp6_0/1037581

        $template .= <<<EOF
        
        
    protected \$schema = [
{$this->getSchema()}
    ];
}    
EOF;

        return $template;
    }

    /**
     * @param array $readonly
     * @return $this
     */
    public function setReadOnly($readonly = [])
    {
        $this->readonly = $readonly;

        return $this;
    }

    /**
     * @param $pk
     * @return $this
     */
    public function setPk($pk)
    {
        $this->pk = $pk;

        return $this;
    }

    /**
     * @return bool
     */
    public function getOnly()
    {
        $this->readonly = config('database.readonly');

        if (empty($this->readonly)) {
            return false;
        }

        if (is_string($this->readonly)) {
            $this->readonly = explode(',', $this->readonly);
        }

        if ($this->pk && $this->pk !== 'id') {
            $this->readonly = array_merge($this->readonly, [
                'id',
                'create_time',
                $this->pk,
            ]);
        }

        if ($this->readonly) {

            $fields = concatArrayFormatNoKey($this->readonly);

            return 'protected $readonly = ' . $fields . ';';
        }

        return true;
    }

    /**
     * 设置获取器信息
     * @return string
     */
    public function getAttr()
    {

        $template = '' . "\n";

        $columns = tableColumns($this->parameter->getName());

        if ($columns) {
            foreach ($columns as $column) {

                $field = $column['Field'];

                $this->property[] = [
                    'type' => $column['Type'],
                    'field' => $field,
                ];

                if (isSatisfyConstant($column)) {

                    $action = transToPascal($field);

                    $name = transToPascal($this->parameter->getName());

                    $template .= <<<EOT
    /**
     * @param \$value
     * @param \$data
     * @return array|mixed|string
     */                
    protected function get{$action}TextAttr(\$value, \$data)
    {
        return {$name}Logic::instance()->trans$action(\$data['$field']);
    }
EOT;
                    $template .= "\n\n";
                }
            }
        }

        return rtrim($template, "\n");
    }

    /**
     * 获取model类的属性信息
     * @return string
     */
    private function getProperty()
    {
        return appendModelClassProperty($this->property, $this->className(), $this->getFileNamespace());
    }

    private function getSchema()
    {
        $columns = tableColumns($this->parameter->getName());

        $template = "\n";
        if ($columns) {
            foreach ($columns as $column) {

                $field = $column['Field'];

                if (preg_match('/int/', $column['Type'])) {
                    $type = "integer";
                } elseif (preg_match('/float/', $column['Type'])) {
                    $type = "float";
                } elseif (preg_match('/decimal/', $column['Type'])) {
                    $type = "float";
                } elseif (preg_match('/char/', $column['Type'])) {
                    $type = "string";
                } else {
                    $type = "string";
                }

                $template .= "        '{$field}' => '{$type}',\n";
            }
        }

        $template = trim($template, "\n");

        return $template;
    }
}